<?php
/**
 * @filename ProgressController.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2009-12-19 16:57:37
 * @version 1.0
 * @Description 进度
 */
class ProgressController extends FrontController
{
    private $pagesize = 15;
    /**
     * 项目级别对标进度
     */
    public function actionIndex()
    {
        $p = $this->get('p', 1);
        $project = new Project();
        $network = new NetWork();
        $netquestion = new NetworkQuestion();
        
        $project_list = $project_ids = $network_ids =array();
        
        if($this->auth_grade == 1) {//如果是总行,得到下级网点,分行、支行
            $network_list = $network->findAll("bank_id={$this->bank_id}");  
        } else{
            $network_list = $network->getAllNext($this->network_id, $this->auth_grade);
        }
        //网点问卷
        foreach ($network_list as $key => $val) {
            $network_ids[] = $val['network_id'];
        }
        $netquestion_list = $netquestion->findAll('network_id in (' . join(',', $network_ids) .') and status='.NetworkQuestion::NETWORK_QUESTION_NORMAL);
        foreach ($netquestion_list as $key => $val) {
            $project_ids[] = $val['project_id'];
        }
        if (empty($project_ids)) {
            $this->smarty->display('error.tpl');exit;
        }
        //得到下级网点项目所有
        $project_list = $project->findAll('project_id in ('.join(',',$project_ids).') and status='.Project::PROJECT_RUNNING);

        $this->smarty->assign('projectlist', $project_list);
        $this->smarty->display('progress/index.tpl');
    }
    /**
     * 问卷级别的进度
     */
    public function actionQuestion()
    {
        $p = $this->get('p', 1);
        //去掉项目那一级别
        $project = new Project();
        $network = new NetWork();
        
          
        $network_ids = array();
        if($this->auth_grade == 1) {//如果是总行,得到下级网点,分行、支行
            $network_list = $network->findAll("bank_id={$this->bank_id}");  
        } else{
            $network_list = $network->getAllNext($this->network_id, $this->auth_grade);
        }
        if ($network_list) {
            //网点问卷
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
            }
        }
        
        $netquestion = new NetworkQuestion();
        $netquestion_list = $netquestion->findAll('network_id in (' . join(',', $network_ids) .') and status='.NetworkQuestion::NETWORK_QUESTION_NORMAL);
        foreach ($netquestion_list as $key => $val) {
            $question_ids[] = $val['question_id'];
            unset($netquestion_list[$key]);
        }
       $a = array_unique($question_ids);
        $criteria = new CDbCriteria();
        $criteria->addInCondition('question_id', array_unique($question_ids));
        $criteria->addColumnCondition(array('status' => QuestionBase::QUESTION_RELEASE));
        $question = new QuestionBase();
        $count = $question->count($criteria);
        
        $criteria->limit = $this->pagesize;
        $criteria->offset = ($p - 1) * $this->pagesize;
        $list = $question->findAll($criteria);
        
        $network = new NetWork();
        //计算进度，此级别下网点，已经提交到这个级别的
        if ($list) {
            if ($this->level == 3) {
                $level = array(1,2,3);
            } elseif ($this->level == 2) {
                $level = array(1,2);
            } elseif ($this->level == 1) {
                $level = array(1);
            }
            
            foreach ($list as $key => $val) {
                $criteria = new CDbCriteria();
                $criteria->addColumnCondition(array('question_id' => $val['question_id']));
                $criteria->addColumnCondition(array('status' => NetworkQuestion::NETWORK_QUESTION_NORMAL));
                $criteria->addInCondition('network_id', $network_ids);
                $total = NetworkQuestion::model()->count($criteria);
                $criteria->addInCondition('node_id', $level);
                $curr_node = NetworkQuestion::model()->count($criteria);
                $list[$key]['complete_ratio'] = $total==0 ? '0%' : number_format($curr_node/$total*100,2).'%';
            }
        }
        
        
        $page = new Page($count, $criteria->limit);  
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('list', $list);
        $this->smarty->display('progress/question.tpl'); 
    }
    /**
     * 网点具体问卷的进度
     * 只显示没有完成的网点
     */
    public function actionNetwork()
    {
        $p = $this->get('p', 1);
        $question_id = (int)$this->get('question_id');
        //得到问卷信息
        $question = new QuestionBase();
        $info = $question->findByPk($question_id);
        $this->smarty->assign('info', $info);
        
        //项目信息
        $project = new Project();
        $network = new NetWork();
        $project_info = $project->findByPk($info['project_id'])->attributes;
        $network_ids = array();
        if($this->auth_grade == 1) {//如果是总行,得到下级网点,分行、支行
            $network_list = $network->findAll("bank_id={$this->bank_id}");  
        } else{
            $network_list = $network->getAllNext($this->network_id, $this->auth_grade);
        }
        if ($network_list) {
            //网点问卷
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
            }
        }
        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('question_id' => $question_id));
        $criteria->addInCondition('status',array(NetworkQuestion::NETWORK_QUESTION_NORMAL,  NetworkQuestion::NETWORK_QUESTION_COMPLETE));
        if ($network_ids) {
            $criteria->addInCondition('network_id', $network_ids);
        }
        
        $networkquestion = new NetworkQuestion();
        $count = $networkquestion->count($criteria);
        $criteria->limit = $this->pagesize;
        $criteria->offset = ($p - 1) * $this->pagesize;
        $list = $networkquestion->findAll($criteria);
        $page = new Page($count, $this->pagesize);  
        $this->smarty->assign('list', $list);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('project_info', $project_info);
        $this->smarty->assign('audit', NetworkQuestion::$audit_show);
        $this->smarty->assign('level_show',  NetWork::networkLevel());
        $this->smarty->display('progress/network.tpl');
    }
}
